package in.muton.edushare.poll.panels;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.panels.assignment.CreateAssignment;
import in.muton.edushare.poll.pages.NewPollPage;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.PollStatsDAO;
import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Users;

public class UserPollPanel extends EdusharePanel {
	private static final long serialVersionUID = 4967193890654090234L;
	private static final Log log = LogFactory.getLog(UserPollPanel.class);

	public UserPollPanel(String id, IModel<Users> model) {
		super(id, model);
		add(CSSPackageResource.getHeaderContribution(CreateAssignment.class,
				"assignment.css"));
		this.userModel = model;
		Form<Void> f = null;
		byMe = new PollsDAO(getORMInterface()).getPollsByCreator(
				userModel.getObject()).size() > 0
				|| (new PollsDAO(getORMInterface())
						.getPollsByUser(getLoggedInUser())).size() == 0;

		add(f = new Form<Void>("viewSwapForm"));
		Button b;
		f.add(b = new Button("byMe") {
			@Override
			public void onSubmit() {
				byMe = true;
			}

			private static final long serialVersionUID = 4810689610983719158L;
		});
		b.add(new AttributeAppender("class", new Model<String>() {
			private static final long serialVersionUID = -8917454969068557064L;

			@Override
			public String getObject() {
				if (!byMe)
					return "ui-state-default";
				return "ui-state-disabled ui-state-hover";
			}
		}, " "));

		f.add(b = new Button("forMe") {
			@Override
			public void onSubmit() {
				byMe = false;
			}

			private static final long serialVersionUID = 4810689610983719158L;
		});

		b.add(new AttributeAppender("class", new Model<String>() {
			private static final long serialVersionUID = -8917454969068557064L;

			@Override
			public String getObject() {
				if (byMe)
					return "ui-state-default";
				return "ui-state-disabled ui-state-hover";
			}
		}, " "));

		f.add(new Button("newPoll") {
			private static final long serialVersionUID = 8307881572437206963L;

			@Override
			public void onSubmit() {
				setResponsePage(new NewPollPage(new PageParameters()));
			}
		});

		add(new ListView<IModel<Poll>>("pollList", new InnerList<Poll>() {
			private static final long serialVersionUID = -5285710472315837231L;

			@Override
			public List<IModel<Poll>> getObject() {
				List<Poll> pollList;
				if (byMe) {
					pollList = new PollsDAO(getORMInterface())
							.getPollsByCreator(userModel.getObject());
				} else {
					pollList = new PollsDAO(getORMInterface())
							.getPollsByUser(getLoggedInUser());
					Iterator<Poll> it = pollList.iterator();
					while (it.hasNext()) {
						Poll p = it.next();
						if (p.getStatus() == Poll.POLL_STATUS_DRAFT) {
							it.remove();
						}
					}
				}
				Collections.sort(pollList);
				Collections.reverse(pollList);
				log.debug("Poll list starts");
				for (Poll p : pollList) {
					log
							.debug("Poll name = "
									+ p.getPollAudi(getORMInterface()));
				}
				return new CayenneList<Poll>(pollList).getObject();
			}
		}) {
			@Override
			protected void populateItem(ListItem<IModel<Poll>> arg0) {
				log.debug("Poll name in populate= "
						+ arg0.getModelObject().getObject().getPollAudi(
								getORMInterface()));
				arg0
						.add(new PollListItem("pollListItem", arg0
								.getModelObject()));
			}

			private static final long serialVersionUID = -6528140893176796628L;
		});

		PollStatsDAO psDAO = new PollStatsDAO(getORMInterface());
		WebMarkupContainer wmcByMe = new WebMarkupContainer("statsPollsByMe") {
			@Override
			public boolean isVisible() {
				return byMe;
			}

			private static final long serialVersionUID = -6305525687093968117L;
		};

		add(wmcByMe);
		wmcByMe.add(new Label("totalByMe", Integer.toString(psDAO
				.getTotalUserCreatedPolls(getLoggedInUser()))));
		wmcByMe.add(new Label("openByMe", Integer.toString(psDAO
				.getOpenUserCreatedPolls(getLoggedInUser()))));
		wmcByMe.add(new Label("closedByMe", Integer.toString(psDAO
				.getClosedUserCreatedPolls(getLoggedInUser()))));
		wmcByMe.add(new Label("draftByMe", Integer.toString(psDAO
				.getDraftUserCreatedPolls(getLoggedInUser()))));

		WebMarkupContainer wmcForMe = new WebMarkupContainer("statsPollsForMe") {
			private static final long serialVersionUID = -2018488356437010389L;

			@Override
			public boolean isVisible() {
				return !byMe;
			}
		};

		add(wmcForMe);
		wmcForMe.add(new Label("totalForMe", Integer.toString(psDAO
				.getTotalUserRespondedPolls(getLoggedInUser()))));
		wmcForMe.add(new Label("openForMe", Integer.toString(psDAO
				.getOpenUserRespondedPolls(getLoggedInUser()))));
		wmcForMe.add(new Label("closedForMe", Integer.toString(psDAO
				.getClosedUserRespondedPolls(getLoggedInUser()))));

		add(new Label("label", new Model<String>() {
			private static final long serialVersionUID = 7353710882483860498L;

			@Override
			public String getObject() {
				if (byMe)
					return "Polls created by me";
				return "Polls for me";
			}
		}));

	}

	private boolean byMe = false;
	private IModel<Users> userModel = null;
}
